package operations;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;


/**
 * The Class MacroOperations.
 * This class performs all the operations related to the macro.
 */
public class MacroOperations  {
	protected int global=0;
	protected int count=0;
	protected String Storedmacro;
	public void updatecaret(JTextArea text, JTextArea macrotextarea){
		
		if(global==1){
		    try {
		          macrotextarea.append(text.getText(text.getCaretPosition() - 1, 1));
		          ++count;}
		        catch (Exception e) {}
		    }
		else if(global==2){
			try{
				macrotextarea.insert(text.getText(text.getCaretPosition()-1, 1), text.getCaretPosition()-1);
		    }
		    catch(Exception e){}
		    }
	}
	
	public String TextKeyTyped(JTextArea text,JTextArea macrotextarea,KeyEvent e) {
		if((int)e.getKeyChar()==26){
			global=1;
		    macrotextarea.setText("");
		    try {
		    	if(text.getLineCount()>1){
		    		for (int i = 0; i < text.getLineCount()-1; i++) {
		    			for(int j=0;j<text.getLineEndOffset(i)-1;j++){
		    				macrotextarea.append(" ");
		    				}
		                	macrotextarea.append("\n");
		                	}
		    		}
		    	else{
		    		for(int i=0; i<text.getLineEndOffset(0);i++)
		                			{macrotextarea.append(" ");
		                			}
		                			}
		                		} catch (BadLocationException ex) {
		                			Logger.getLogger(JFrame.class.getName()).log(Level.SEVERE, null, ex);
		                			}
		                		}
		                else if((int)e.getKeyChar()==24){
		                	Storedmacro= macrotextarea.getText().toString().trim();
		                	global=0;
		                	}
			return Storedmacro;
			}
	public void TextKeyPressed(JTextArea text,JTextArea macrotextarea,KeyEvent e) {
		
		if(e.getKeyCode()==37){
				global=3;
				}
		else if(e.getKeyCode()==38){
		    	global=3;
		    	}
		else if(e.getKeyCode()==39){
		        global=3;
		        }
		else if(e.getKeyCode()==40){
			    global=3;
			    }
		else if(e.getKeyCode()==8){
			global=3;
			try{
				macrotextarea.replaceRange("",text.getCaretPosition()-1,text.getCaretPosition());
				}
			catch(Exception ea){}
			}
		else{
		   if(global!=0)
		      global=2;}
		}
	
	 public void PlayRecording(JTextArea text,String Storedmacro,int index){    			
			text.insert(Storedmacro,index);
			}
	 public void mousePressed(MouseEvent e) {
			if(global!=0){
			   global=3;}
	}
	}
